﻿using DataStruct;
using MyDiskInfo;
using MyDiskInfo.DataStruct;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Utils;
using WCFServer;

namespace MyDiskInfoTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        #region 字段
        private System.Timers.Timer _timerForCpu = null;
        private System.Timers.Timer _timerForDisk = null;
        private int _intervalForCpu = 1000; //刷新间隔
        private int _intervalForDisk = 10 * 1000; //刷新间隔
        private IClientServer _client = null;
        private DiskModel2 _diskInfo = null;
        private CpuModel _cpuInfo = null;
        private DiskInfoViewModel _model = new DiskInfoViewModel();
        #endregion

        public MainWindow()
        {
            InitializeComponent();

            BackWork.RunAsync(() =>
            {
                LogUtil.Log("ClientServer 开始启动");

                _client = new ClientServer();

                LogUtil.Log("ClientServer 启动成功");
            }, () =>
            {
                this.DataContext = _model;

                _timerForCpu = new System.Timers.Timer();
                _timerForCpu.Interval = _intervalForCpu;
                _timerForCpu.Elapsed += _timerForCpu_Elapsed;
                _timerForCpu.Start();

                _timerForDisk = new System.Timers.Timer();
                _timerForDisk.Interval = _intervalForDisk;
                _timerForDisk.Elapsed += _timerForDisk_Elapsed;
                _timerForDisk.Start();

                ShowDiskInfo();
            }, ex =>
            {
                LogUtil.Error(ex, "DiskInfoCtrl 构造函数 错误");
            });
        }

        #region _timerForCpu_Elapsed
        private void _timerForCpu_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ShowCpuInfo();
        }
        #endregion

        #region _timerForDisk_Elapsed
        private void _timerForDisk_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ShowDiskInfo();
        }
        #endregion

        #region 显示CPU信息
        private void ShowCpuInfo()
        {
            BackWork.RunAsync(() =>
            {
                _cpuInfo = _client.GetCpuInfo();
            }, () =>
            {
                _model.CpuInfo = _cpuInfo;
            });
        }
        #endregion

        #region 显示硬盘信息
        /// <summary>
        /// 显示硬盘信息
        /// </summary>
        private void ShowDiskInfo()
        {
            BackWork.RunAsync(() =>
            {
                _diskInfo = _client.GetDiskInfo();
            }, () =>
            {
                _model.DiskInfo = _diskInfo;
            });
        }
        #endregion

        #region 释放资源
        public void ClearResources()
        {
            if (_timerForCpu != null)
            {
                _timerForCpu.Stop();
                _timerForCpu.Elapsed -= _timerForCpu_Elapsed;
                _timerForCpu.Close();
                _timerForCpu = null;
            }

            if (_timerForDisk != null)
            {
                _timerForDisk.Stop();
                _timerForDisk.Elapsed -= _timerForDisk_Elapsed;
                _timerForDisk.Close();
                _timerForDisk = null;
            }

            LogUtil.Log("客户端释放资源");
        }
        #endregion

    }
}
